package com.example.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mockito.MockitoFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.common.RespBean;
import com.example.demo.permission.menu.pojo.Menu;
import com.example.demo.permission.menu.service.MenuService;
import com.example.demo.permission.menurole.service.MenuRoleService;
import com.example.demo.permission.role.pojo.Role;
import com.example.demo.permission.role.service.RoleService;

@RestController
@RequestMapping("/system/basic")
public class SystemBasicController {

	@Autowired
	RoleService roleService;

	@Autowired
	MenuService menuService;

	@Autowired
	MenuRoleService menuRoleService;

	@RequestMapping(value = "/role/{rid}", method = RequestMethod.DELETE)
	public RespBean deleteRole(@PathVariable Long rid) {
		if (roleService.deleteRoleById(rid) == 1) {
			return RespBean.ok("删除成功");
		}
		return RespBean.error("删除失败");
	}

	
	@RequestMapping(value = "/addRole", method = RequestMethod.POST)
	public RespBean addNewRole(String role, String roleZh) {
		if (roleService.addNewRole(role, roleZh) == 1) {
			return RespBean.ok("添加成功");
		}
		return RespBean.error("添加失败");
	}
	
	@RequestMapping(value = "/menuTree/{rid}", method = RequestMethod.GET)
	public Map<String, Object> menuTree(@PathVariable Long rid) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Menu> menus = menuService.menuTree();
		map.put("menu", menus);
		List<Long> list = menuService.getMenuByRid(rid);
		map.put("mids", list);
		return map;
	}
	

	@RequestMapping(value = "/updateMenuRole", method = RequestMethod.PUT)
	public RespBean updateMenuRole(Long rid, Long[] mids) {
		if (menuRoleService.updateMenuRole(rid, mids) == mids.length) {
			return RespBean.ok("修改成功");
		}
		return RespBean.error("修改失败");
	}

	@RequestMapping("/roles")
	public List<Role> allRoles() {
		List<Role> list = new ArrayList<Role>();
		list = roleService.roles();
		return list;
	}

}
